// pages/recruitWorkExpAdd/recruitWorkExpAdd.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: true, label: '公司名称', key: 'companyName', valueType: 'text', tips: '请填写', type: 'input' },
      { isrequire: true, label: '开始时间', key: 'beginDate', valueType: 'text', tips: '请选择', type: 'select', mode: 'date' },
      { isrequire: true, label: '结束时间', key: 'endDate',start:'', valueType: 'text', tips: '请选择', type: 'select', mode: 'date' },
      { label: '', type:'label'},
      { isrequire: true, label: '岗位名称', key: 'positionName', valueType: 'text', tips: '请填写', type: 'input' },
      { isrequire: true, label: '职位薪资', index: [0, 0], key: 'salary', valueType: 'text', tips: '请选择,单位K', type: 'select', mode: 'multiSelector',data:[]},
      { isrequire: true, label: '工作描述', key: 'workDescrip',showData:true, valueType: 'text', tips: '请填写', type: 'input', url: "/pages/jobDescrip/jobDescrip?page=1"},
    ],
    formData: {
      companyName: '',
      beginDate: "",
      endDate: '',
      positionName: '',
      salary: '',
      workDescrip: ''
    },
    showData:{workDescrip:'已填写'},
    isEdit: false,
    canSubmit: false,
    now:''
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      this.data.formData[item.key] = item.data[e.detail.value]
    } else {
      this.data.formData[item.key] = e.detail.value;
      if (item.key =='beginDate'){
        this.setData({
          ['formlist[2].start']: e.detail.value
        })
      }
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  bindMultiPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.formlist[index].index = e.detail.value;
    this.data.formData[item.key] = item.data[0][e.detail.value[0]] + 'K-' + item.data[1][e.detail.value[1]] + 'K';
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  bindColumnchange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    if (e.detail.column == 0) {
      var arr = [];
      arr = arr.concat(item.data[0].slice(e.detail.value, item.data[0].length));
      this.setData({
        ['formlist[' + index + '].data[1]']: arr
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      now: util.getNow().substring(0, 7)
    })
    var that = this;
    // if(options.value){
    //   if (options.isEdit) {
    //     this.setData({
    //       isEdit: true
    //     })
    //   }
    //   wx.getStorage({
    //     key: 'workInfo',
    //     success: function (res) {
    //       for (var j in that.data.formData) {
    //         if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
    //           that.data.formData[j] = res.data[j];
    //         }
    //       }
    //       if (options.isEdit) {
    //         that.data.formData.id = res.data.id;
    //       }
    //       that.data.formData.workDescrip = options.value;
    //       that.setData({
    //         formData: that.data.formData
    //       })
    //       that.getMonthIncome();
    //       that.changeValue();
    //       wx.removeStorage({
    //         key: 'workInfo'
    //       })
    //     }
    //   })
    // }else{
      if (options.isEdit) {
        this.setData({
          isEdit: true
        })
        wx.getStorage({
          key: 'infoItem',
          success: function (res) {
            for (var j in that.data.formData) {
              if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
                that.data.formData[j] = res.data[j];
              }
            }
            that.data.formData.id = res.data.id;
            that.setData({
              formData: that.data.formData
            })
            that.getMonthIncome();
            that.changeValue();
            wx.removeStorage({
              key: 'infoItem'
            })
          }
        })
      } else {
        this.getMonthIncome();
      }
    // }
  },
  getMonthIncome: function () {
    var arr = [];
    for (var i = 1; i <= 50; i++) {
      arr.push(i);
    }
    arr.push('50k以上');
    if (this.data.formData.salary != '') {
      if (this.data.formData.salary.indexOf('-') >= 0) {
        var monthIncome = this.data.formData.salary.split('-');
        var heightiIndex = [0, 0];
        arr.forEach(function (ele, index) {
          if (ele == monthIncome[0].substring(0, monthIncome[0].length - 1)) {
            heightiIndex[0] = index;
          }
          if (ele == monthIncome[1].substring(0, monthIncome[1].length - 1)) {
            heightiIndex[1] = index;
          }
        })
        this.setData({
          ['formlist[5].index']: heightiIndex,
        })
      }
    }
    this.setData({
      ['formlist[5].data']: [arr, arr]
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  inputTap: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url + '&value=' + this.data.formData.workDescrip
    })
    // if (this.data.isEdit){
    //   wx.navigateTo({
    //     url: e.currentTarget.dataset.url + '&value=' + this.data.formData.workDescrip+'&isEdit=true'
    //   })
    // }else{
    //   wx.navigateTo({
    //     url: e.currentTarget.dataset.url + '&value=' + this.data.formData.workDescrip
    //   })
    // }
  },
  saveWorkInfo: function (e) {
    if (this.data.canSubmit) {
      var param = {};
      var url = '';
      
      if (this.data.isEdit) {
        url = 'recruitWorkInfo/update';
      } else {
        url = 'recruitWorkInfo/save';
        param.userId = this.data.userInfo.id;
      }
      for (var j in this.data.formData) {
        param[j] = this.data.formData[j];
      }
      var that = this;
      wx.request({
        url: app.globalData.httpApiCar + url, //仅为示例，并非真实的接口地址
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: param,
        success: function (res) {
          if (res.data.success) {
            wx.navigateBack({
              delta:1
            })
          }
        }
      })
    }
  },
  del: function (e) {
    wx.request({
      url: app.globalData.httpApiCar + 'recruitWorkInfo/del', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: { id: this.data.formData.id },
      success: function (res) {
        if (res.data.success) {
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showToast({
            title: '删除失败',
            duration: 2000
          })
        }
      }
    })
  },
  saveCanel: function (e) {
    wx.redirectTo({
      url: '/pages/blindMine/blindMine'
    })
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.companyName == '') || (item.beginDate == '') || (item.endDate == '') || (item.positionName == '') || (item.salary == '') || (item.workDescrip == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getStorage({
      key: 'descripton',
      success: function(res) {
        that.setData({
          'formData.workDescrip': res.data
        })
        that.changeValue();
        wx.removeStorage({
          key: 'descripton',
          success: function(res) {},
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})